package wang.dao;

import wang.bean.Admin;
import wang.utils.JDBCUtils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;

public class AdminDao {
  public static final String FIELD_ID = "id";
  public static final String FIELD_NAME = "name";
  public static final String FIELD_USERNAME = "username";
  public static final String FIELD_SECRET_KEY = "secret_key";

  // 新增管理员
  public void insert(Admin admin) {
    Connection conn = null;
    PreparedStatement ps = null;
    ResultSet rs = null;
    try {
      // 获得数据的连接
      conn = JDBCUtils.getConnection();

      // 发送SQL语句
      String sql = "insert into t_admin(username, secret_key, name) values(?, ?, ?)";

      ps = conn.prepareStatement(sql);
      ps.setString(1, admin.getUsername());
      ps.setString(2, admin.getSecretKey());
      ps.setString(3, admin.getName());
      int num = ps.executeUpdate();
    } catch (Exception e) {
      e.printStackTrace();
    } finally {
      JDBCUtils.release(rs, ps, conn);
    }
  }

  // 删除管理员
  public void delete(int id) {
    Connection conn = null;
    Statement stmt = null;
    ResultSet rs = null;
    try {
      conn = JDBCUtils.getConnection();
      stmt = conn.createStatement();
      String sql = "DELETE FROM t_admin WHERE id = " + id;
      int num = stmt.executeUpdate(sql);
    } catch (Exception e) {
      e.printStackTrace();
    } finally {
      JDBCUtils.release(rs, stmt, conn);
    }
  }

  // 修改管理员
  public boolean update(Admin admin) {
    Connection conn = null;
    PreparedStatement ps = null;
    ResultSet rs = null;
    try {
      conn = JDBCUtils.getConnection();
      String sql = "update t_admin set username = ?, secret_key = ?, name = ? where id=?";
      ps = conn.prepareStatement(sql);
      ps.setString(1, admin.getUsername());
      ps.setString(2, admin.getSecretKey());
      ps.setString(3, admin.getName());
      ps.setInt(4, admin.getId());
      int num = ps.executeUpdate();
      return num > 0;
    } catch (Exception e) {
      e.printStackTrace();
    } finally {
      JDBCUtils.release(rs, ps, conn);
    }
    return false;
  }

  // 获取管理员
  public Admin get(int id) {
    Connection conn = null;
    Statement stmt = null;
    ResultSet rs = null;
    try {
      // 获得数据的连接
      conn = JDBCUtils.getConnection();
      // 获得Statement对象
      stmt = conn.createStatement();
      // 发送SQL语句
      String sql = "SELECT * FROM t_admin WHERE id = " + id;
      rs = stmt.executeQuery(sql);
      // 处理结果集
      while (rs.next()) {
        Admin admin = new Admin();
        admin.setId(rs.getInt(FIELD_ID));
        admin.setUsername(rs.getString(FIELD_USERNAME));
        admin.setSecretKey(rs.getString(FIELD_SECRET_KEY));
        admin.setName(rs.getString(FIELD_NAME));
        return admin;
      }
      return null;
    } catch (Exception e) {
      e.printStackTrace();
    } finally {
      JDBCUtils.release(rs, stmt, conn);
    }
    return null;
  }

  // 根据账号查找指定的管理员
  public Admin findByUsername(String username) {
    Connection conn = null;
    Statement stmt = null;
    ResultSet rs = null;
    try {
      // 获得数据的连接
      conn = JDBCUtils.getConnection();
      // 获得Statement对象
      stmt = conn.createStatement();
      // 发送SQL语句
      String sql = "SELECT * FROM t_admin WHERE username = '" + username + "'";
      rs = stmt.executeQuery(sql);
      // 处理结果集
      while (rs.next()) {
        Admin admin = new Admin();
        admin.setId(rs.getInt(FIELD_ID));
        admin.setUsername(rs.getString(FIELD_USERNAME));
        admin.setSecretKey(rs.getString(FIELD_SECRET_KEY));
        admin.setName(rs.getString(FIELD_NAME));
        return admin;
      }
      return null;
    } catch (Exception e) {
      e.printStackTrace();
    } finally {
      JDBCUtils.release(rs, stmt, conn);
    }
    return null;
  }
}